﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Diagnostics;

namespace SDFL.Remoting
{
    public class Helper
    {
        // ping the remote computer 
        public static bool VerifyRemoteMachineStatus(string machineName)
        {
            try
            {
                using (Ping ping = new Ping())
                {
                    PingReply reply = ping.Send(machineName);
                    if (reply.Status == IPStatus.Success)
                    { return true; }
                }
            }
            catch (Exception ex)
            {
                // return false for any exception encountered
                // we'll probably want to just shut down anyway
            }
            return false;
        }

        // machine can have multiple nic cards (and laptops usually have wifi & wired) 
        // don't want to get into figuring out which one to show, just get all
        // can enumerate network card other ways (System.Net.NetworkInformation) 

        // PerformanceCounters can return string[] of available network interfaces
        public static string[] GetNICInstances(string machineName)
        {
            string filter = "MS TCP Loopback interface";
            List<string> nics = new List<string>();
            PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface", machineName);
            if (category.GetInstanceNames() != null)
            {
                foreach (string nic in category.GetInstanceNames())
                {
                    if (!nic.Equals(filter, StringComparison.InvariantCultureIgnoreCase))
                    { nics.Add(nic); }
                }
            }
            return nics.ToArray();
        }

        // create a Performance Counter for each network interface
        public static PerformanceCounter[] GetNICCounters(string machineName)
        {
            string[] nics = GetNICInstances(machineName);
            List<PerformanceCounter> nicCounters = new List<PerformanceCounter>();
            foreach (string nicInstance in nics)
            {
                nicCounters.Add(new PerformanceCounter("Network Interface", "Bytes Total/sec", nicInstance, machineName));
            }
            return nicCounters.ToArray();
        }
    }
}
